<?php
/*
	首页
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
set_html('header','common/header-cms');
if($_RQ['op']=='main'){
	$return['list']=array();
	$return['indexpai']=array();	
	$setting = $return['setting'];
	$return['welcomehdps']=$setting['welcomehdps'];
	if(count($setting['welcomehdps'])>0 && $setting['iswelcome']==1){
		$return['iswelcome']=$this->session('iswelcome')?0:1;
		if($return['iswelcome']){
			$this->session('iswelcome',1);
		}
	}
	if(!$setting['issearch']){
		$return['indexpai'][]=array('name'=>'search');
	}
	if(!$setting['ishdps']){
		$return['indexpai'][]=array('name'=>'hdps');
	}
	$return['indexpai'][]=array('name'=>'notice');
	if(!$setting['isswiper']){
		$return['indexpai'][]=array('name'=>'swiper');
	}
	if(!$setting['isbankuai1']){
		$return['indexpai'][]=array('name'=>'bankuai1');
	}
	if(!$setting['isgroups']){
		$return['indexpai'][]=array('name'=>'groups');
	}
	if(!$setting['iscube']){
		$return['indexpai'][]=array('name'=>'cube');
	}
	if(!$setting['isshare']){
		$return['indexpai'][]=array('name'=>'share');
	}
	if(!$setting['no_ordermodal']){
		$return['indexpai'][]=array('name'=>'ordermodal');
	}
	$return['indexpai'][]=array('name'=>'coupon');
	$return['indexpai'][]=array('name'=>'goods');
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'isrecommand'=>'1','isshow'=>'1','mid'=>0,'parentid'=>0);
	$groupspram['onepluginname']='';
	$p_group=pdo_getall('jy_weishop_goods_group',$groupspram,'','','paixu desc');
	//子分类
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	$params['saletype likes']=array('1');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','image','price','total','active_plugin','active_id');
	if($p_group){
		$i=0;
		foreach($p_group as &$item){
			$item['c_group']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isshow'=>'1','isrecommand'=>'1','parentid'=>$item['id']),'','paixu asc');
			if($_SESSION['os']=='windows'){
				$item['goodslist']=pdo_getslice('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'saletype likes'=>array('1'),'pcate'=>$item['id']),array($pindex,$psize),$total,$fields,'','paixu desc');
				$item['goodsnew']=pdo_getslice('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'saletype likes'=>array('2'),'pcate'=>$item['id']),array($pindex,$psize),$total,$fields,'','paixu desc');
			}
			$item['style']=style($item['id']);
			$i=$i+1;
			$item['i']=$i;
			if($item['c_group']){
				foreach($item['c_group'] as &$row){
					$params['ccate']=$row['id'];
					if($_SESSION['os']=='windows'){
						$row['goods']=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','paixu desc');
					}
				}
			}
		}
	}
	//公告列表
	$notice_list = pdo_getslice('jy_weishop_notice',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array($pindex,20),$total,array('id','title','createtime'),'','id desc');
	if($notice_list){
		foreach($notice_list as &$notice){
			if($_SESSION['os']=='windows'){
				$notice['createtime']=date('m-d',$notice['createtime']);
			}else{
				$notice['createtime']=date('Y-m-d h:i',$notice['createtime']);
			}
		}
	}
	$pindex = max(1, intval($post['page']));
	$psize = 10;
	$total='';
	//获取用户信息
	$return['extradata']=array();
	$return['user']=model('core/member')->basic($_SESSION['uid']);
	$return['login']=$_SESSION['uid']?1:2;
	$return['notice_list']=$notice_list;
	$return['p_group']=$p_group;
	$return['params']['pid']=0;
	$return['feemoney']=plugin('jy_weishop')->plugin_setting('feemoney');
	$return['pcd']=$this->session('pcd')?:array('city'=>'');
	$jy_merch = $this->plugin_exists('jy_merch');
	if($jy_merch){
		$return['jy_merch']=true;
	}
	set_html('title',$return['shopset']['title']?:'首页-在线商城');
	$sale_luck=plugin('jy_weishop')->plugin_setting('sale_luck');
	if($sale_luck['isopen']==1){
			if(isset($_RQ['luckkey'])){
				$return['sale_luck']=model('jy_weishop/activity/sales')->jieluck($_RQ['luckkey']);
			}else{
				$return['sale_luck']=model('jy_weishop/activity/sales')->getluck();
			}
	}
	if(!$return['sale_luck']){
			$isshow_sale_offer=$this->session('isshow_sale_offer');
			if(!$isshow_sale_offer){
				$return['sale_offer']=model('jy_weishop/activity/sales')->getoffer();
			}
			if(!$return['sale_offer']){
				$isshow_sale_new=$this->session('isshow_sale_new');
				if(!$isshow_sale_new){
					$return['sale_new']=model('jy_weishop/activity/sales')->getnew();
				}
				if(!$return['sale_new']){
					$return['orderlist']=pdo_getslice('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'deleted'=>0,'status'=>0),array($pindex,$psize),$total,'','','createtime desc');
					foreach($return['orderlist'] as &$ov){
						$ordergoods=pdo_get('jy_weishop_order_goods',array('orderid'=>$ov['id'],'uniacid'=>$_SESSION['uniacid']),array('goodsid','optionid','price','total','goodssn','productsn'));
						$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$ordergoods['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','image','title','merchid'));
						$ov['goods']=array_merge($ordergoods,$goodsinfo);
						$ov['createtime']=date('Y/m/d H:i:s',$ov['createtime']);
						$ov['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$ov['id'],'uniacid'=>$_SESSION['uniacid']),'total');
					}
					$return['allprice']=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'deleted'=>0,'status'=>0),'price');
					if(empty($return['orderlist'])){
						$return['coupon_wait']=model('jy_weishop/task')->pop_coupon_wait();
						$return['coupon_wait']=$return['coupon_wait']?$return['coupon_wait'][0]:false;
						if(!$return['coupon_wait']){
							$return['coupon_hd']=model('jy_weishop/task')->pop_coupon_hd();
							if(!$return['coupon_hd']){
								$return['game_wait']=model('jy_weishop/task')->pop_game();
							}
						}
					}
				}else{
					$this->session('isshow_sale_new',1);
				}
			}else{
					$this->session('isshow_sale_offer',1);
			}
	}
	if($return['sale_luck']){
		$sharelink='jy_weishop/index//fromuser='.$_SESSION['uid'].'&luckkey='.$return['sale_luck']['luckkey'];
		if($sale_luck['share_title']){
			$sale_luck['share_title']=str_replace('[lucktitle]',$return['sale_luck']['title'],$sale_luck['share_title']);
		}
		if($sale_luck['share_desc']){
			$sale_luck['share_desc']=str_replace('[lucktitle]',$return['sale_luck']['title'],$sale_luck['share_desc']);
		}
		$return['sharedata']=array(
			'title'=>$sale_luck['share_title']?$sale_luck['share_title']:'我今日运势《'.$return['sale_luck']['title'].'》，快来借势吧,好运势享大折扣！',
			'desc'=>$sale_luck['share_desc']?$sale_luck['share_desc']:'拥有好运势，不仅仅拥有好心情，更能享受好的购物折扣哦！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>$sale_luck['share_image']?tomedia($sale_luck['share_image']):tomedia('../addons/jy_weishop/resource/images/sales/luck_share_image.jpg')
		);
	}else{
		$sharelink='jy_weishop/index//fromuser='.$_SESSION['uid'];
		$return['sharedata']=array(
			'title'=>$setting['share_title']?$setting['share_title']:'首页-在线商城',
			'desc'=>$setting['share_desc']?$setting['share_desc']:'在线商城，一个属于自己的个人商城！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($setting['share_image'])
		);
	}
	$return['nodata_pic']='../core/resource/images/index_nodata.jpg';
	$return['sale_red']=$this->pdo_getall('feelog',array('type'=>5,'status'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	foreach($return['sale_red'] as &$sv){
		$sv['member']=cfc('core')->member_show($sv['mid']);
		$sv['createtime']=date('Y-m-d H:i:s',$sv['createtime']);
	}
	$return['red']=$_SESSION['member'];
	$return['red']['allfee']=$this->pdo_getsum('feelog',array('type'=>5,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fee');
	$return['red']['isfee']=$this->pdo_getsum('feelog',array('type'=>5,'status'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fee');
	$return['red']['lastfee']=$this->pdo_getsum('feelog',array('type'=>5,'status'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fee');
	$jy_merch=plugin('jy_merch')->plugin_setting('setting');
	$return['red']['red_mp3']=$jy_merch['red_mp3'];
	if(intval($setting['index_tpl']) && $_SESSION['os'] != 'windows'){
		$return['htmls']=$this->template_url('index_a'.$setting['index_tpl']);
	}else{
		$return['setting']['index_tpl']=3;
	}
	exi($return);
}
if($_RQ['op']=='getred'){
	$post=get_postdata();	
	$id=intval($post['id']);
	$sale_reds=$this->pdo_getall('feelog',array('type'=>5,'status'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if(empty($sale_reds)){
		exi('没有红包可领取！','error');
	}
	foreach($sale_reds as $sv){
		if($sv['gift_type'] && $sv['fee']>0){
			$red=array(
				'mid'=>$_SESSION['uid'],
				'money'=>$sv['fee'],
				'plugin'=>'jy_pinggou',
				'remark'=>'拼团红包提现',
			);
			model('core/red_pack')->get_money($red);
		}
	}
	$this->pdo_update('feelog',array('status'=>1),array('type'=>5,'status'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	exi('领取成功！');
}
function style($id)
{
	switch ($id%7) {	
		case 0: return "style-brown";
		case 1: return 'style-red';
		case 2: return "style-pink";
		case 3: return "style-orange";
		case 4: return "style-green";
		case 5: return "style-blue";
		case 6: return "style-purple";
		case 7: return "style-brown";
		default: }
}
	
?>